
import {getDrawerList, getDrawerListByCstId, addDrawer, updateDrawer, getDrawerById, deleteDrawerById, addDrawerAndSubmit, updateDrawerAndSubmit, drawerApproval, getSupplygoodsByCstId, getApprovalDrawerList} from '../../services/customer/Drawer';
import Constants from '../../utils/Constants';
import { message } from "antd";
import OperatorConstant from "../../utils/OperatorConstant";
const R = require('ramda');

export default {
    namespace: 'drawer',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list: [],
        supplygoodsList:[],

        dataSource: {},
        drawerStatus: "1",
    },
    reducers: {

        //-----------------setList



        /**
         * 设置开票人列表
         * @param state
         * @param payload
         * @returns {{drawerList: *, drawerPage: (state.drawerPage|{page, size, totalRow, totalPage}|*)}}
         */
        setDrawerList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page, drawerStatus: payload.status}
        },

        /**
         * 设置用户境内货源地
         * @param state
         * @param payload
         * @returns {{supplygoodsList: *}}
         */
        setSupplygoodsList(state, { payload }) {
            return { ...state, supplygoodsList: payload.list}
        },


        //--------------------setEntity



        /**
         * 设置开票人
         * @param state
         * @param action
         * @returns {{drawer: (state.drawer|{}|*)}}
         */
        setDrawer(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.dataSource,};
        },


    },
    effects: {


        //----------------------------开票人


        /**
         * 获取产品信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getDrawerList({payload}, { call, put, select }) {
            let page = yield select(state => state.drawer.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status =  yield select(state => state.drawer.drawerStatus);
            status =  R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;


            const res = yield call(getDrawerList, {
                drawerName: R.isNil(payload) || R.isNil(payload.drawerName) ? "" : payload.drawerName,
                taxplayercode: R.isNil(payload) || R.isNil(payload.taxplayercode) ? "" : payload.taxplayercode,
                status: status,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setDrawerList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status : status,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        *getDrawerListByCstId({payload}, { call, put, select }) {
            let page = yield select(state => state.drawer.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status =  yield select(state => state.drawer.drawerStatus);
            status =  R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;


            const res = yield call(getDrawerListByCstId, {
                cstId: R.isNil(payload) || R.isNil(payload.cstId) ? "" : payload.cstId,
                drawerName: R.isNil(payload) || R.isNil(payload.drawerName) ? "" : payload.drawerName,
                taxplayercode: R.isNil(payload) || R.isNil(payload.taxplayercode) ? "" : payload.taxplayercode,
                status: status,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setDrawerList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status : status,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        *getApprovalDrawerList({payload}, { call, put, select }) {
            let page = yield select(state => state.drawer.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status =  yield select(state => state.drawer.drawerStatus);
            status =  R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;


            const res = yield call(getApprovalDrawerList, {
                cstId: R.isNil(payload) || R.isNil(payload.cstId) ? "" : payload.cstId,
                drawerName: R.isNil(payload) || R.isNil(payload.drawerName) ? "" : payload.drawerName,
                taxplayercode: R.isNil(payload) || R.isNil(payload.taxplayercode) ? "" : payload.taxplayercode,
                status: status,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setDrawerList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page: R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status : status,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据id查询开票人
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getDrawerById({ payload }, { call, put, select }) {
            const res = yield call(getDrawerById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setDrawer",
                    payload: {
                        dataSource: data,
                    }
                });

            }
        },

        /**
         * 删除开票人
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteDrawerById({ payload }, { call, put, select }) {
            let page = yield select(state => state.drawer.page);
            let status =  yield select(state => state.drawer.drawerStatus);

            const res = yield call(deleteDrawerById, {id: payload.id});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("删除开票人成功");

                if(payload.onSuccess())payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加开票人
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *addDrawer({payload}, { call, put, select }) {
            let method = R.isNil(payload.method) ?
                addDrawer : (payload.method == OperatorConstant.saveAndApproval ? addDrawerAndSubmit : addDrawer);

            let page = yield select(state => state.drawer.page);
            let status =  yield select(state => state.drawer.drawerStatus);

            const res = yield call(method, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("增加开票人成功");

                if(payload.onSuccess())payload.onSuccess();


            } else {
                if(payload.onFail())payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新开票人
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *updateDrawer({payload}, { call, put, select }) {
            let method = R.isNil(payload.method) ?
                updateDrawer : (payload.method == OperatorConstant.saveAndApproval ? updateDrawerAndSubmit : updateDrawer);

            let page = yield select(state => state.drawer.page);
            let status =  yield select(state => state.drawer.drawerStatus);

            const res = yield call(method, payload);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("修改开票人成功");

                if(payload.onSuccess())payload.onSuccess();


            } else {
                if(payload.onFail())payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 审核开票人
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *drawerApproval({payload}, { call, put, select }) {

            const res = yield call(drawerApproval, payload);
            let page = yield select(state => state.drawer.page);
            let status =  yield select(state => state.drawer.drawerStatus);

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                message.info("审核开票人成功");

                if(payload.onSuccess)payload.onSuccess();


            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取用户境内货源地
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getSupplygoodsByCstId({ payload }, { call, put, select }) {
            const res = yield call(getSupplygoodsByCstId, { cstId: payload.cstId });

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setSupplygoodsList",
                    payload: {
                        list: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },
    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};